package com.ms.p2p.controller.admin;


import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.common.bean.Message;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.controller.BaseController;
import com.ms.p2p.entity.system.PayMethod;
import com.ms.p2p.entity.system.PayMethodValue;
import com.ms.p2p.plugin.IPlugin;
import com.ms.p2p.plugin.payment.PaymentPluginBundle;
import com.ms.p2p.provider.PayMethodProvider;
import com.ms.p2p.provider.PayMethodValueProvider;
import com.ms.p2p.utils.JsonDataGridHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 *  支付方式管理
 */
@Controller
@RequestMapping({"/admin/payment"})
public class AdminPayMethodController extends AdminBaseController {
	@Autowired
	PaymentPluginBundle paymentPluginBundle;
	@Reference
	private PayMethodProvider payMethodProvider;

	@Reference
	private PayMethodValueProvider payMethodValueProvider;


	@RequestMapping({"/list"})
	public String payments(HttpServletRequest request){
		List<PayMethod> payments = payMethodProvider.find(null);

		return "admin/account/payment/list";
	}

	@RequestMapping(value = {"/queryData"}, produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public PageBean queryData(HttpServletRequest request, HttpSession session
							 , PageBean page
	) {
		page.setList(payMethodProvider.find(null));
		page.setTotalCount(1000);
		return JsonDataGridHelper.createJSONData(page);
	}

	@RequestMapping("/modify")
	@ResponseBody
	public Message modify(HttpServletRequest request) throws IllegalAccessException, InvocationTargetException, IntrospectionException{
			String returnMsg = "";
			String id = request.getParameter("id");
			PayMethod payment = payMethodProvider.findById(id);
			if(payment.getStatus()==0){
				payment.setStatus(1);
				returnMsg = "启用成功";
			}else{
				payment.setStatus(0);
				returnMsg = "暂停成功";
			}
			payMethodProvider.update(payment);
			return Message.success(returnMsg);
	}

	@RequestMapping("/detail")
	public String detail(HttpServletRequest request, @RequestParam(value = "id",required = false) String id
	) {
		PayMethod payment = null;
		if (StringUtils.isNotBlank(id)) {
			payment = payMethodProvider.findById(id);
			request.setAttribute("payment", payment);
			if (payment.getStyle() == 1) {

				request.setAttribute("paymentValues", payMethodValueProvider.findByPaymentId(payment.getId()+""));
				return "/admin/account/payment/setting";


			}
		}
		return "admin/account/payment/input";


	}
	@RequestMapping("/saveValue")
	@ResponseBody
	public Message saveValue(String paymentId, HttpServletRequest request){

		List<PayMethodValue>list = payMethodValueProvider.findByPaymentId(paymentId);

		for(PayMethodValue payMethodValue:list){
			payMethodValue.setParamValue(request.getParameter(payMethodValue.getParamName()));
			payMethodValueProvider.update(payMethodValue);
		}
		return Message.success("操作成功");

	}
	@RequestMapping("/save")
	@ResponseBody
	public Message save(HttpServletRequest request, PayMethod payment) throws IllegalAccessException, InvocationTargetException, IntrospectionException{
		int result = 0;
		if(payment.getId() == null){
			result = payMethodProvider.insert(payment);
		} else {
			result = payMethodProvider.update(payment);
		}

		return Message.success("操作成功");
	}

	@Override
	public boolean getBinderFlag() {
		return true;
	}
}
